package com.googlecode.lookie.platform.image;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils
{

    public final static BufferedImage byteArrayToBufferedImage( final byte[] _pic )
    {
        final ByteArrayInputStream in = new ByteArrayInputStream( _pic );
        final BufferedImage image;
        try
        {
            image = ImageIO.read( in );
            in.close();
            return image;
        }
        catch ( final IOException e )
        {
            e.printStackTrace();
        }
        return null;
    }

    public final static byte[] bufferedImageToByteArray( BufferedImage image )
    {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        try
        {
            ImageIO.write( image, "JPEG", out );
            out.close();
            return out.toByteArray();
        }
        catch ( final IOException e )
        {
            e.printStackTrace();
        }
        return null;
    }

    public final static BufferedImage scaleThumbnail( Image inImage )
    {
        final double scale = Math.min( (double) 300 / (double) inImage.getHeight( null ), (double) 300
            / (double) inImage.getWidth( null ) );

        // Determine scaled size of new image.
        int scaledH, scaledW;
        if ( ( scaledW = (int) ( scale * inImage.getWidth( null ) ) ) <= 0 )
        {
            scaledW = 1;
        }
        if ( ( scaledH = (int) ( scale * inImage.getHeight( null ) ) ) <= 0 )
        {
            scaledH = 1;
        }

        final BufferedImage outImage = new BufferedImage( scaledW, scaledH, BufferedImage.TYPE_INT_RGB );
        final Graphics2D g2d;
        g2d = outImage.createGraphics();
        g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED );
        g2d.drawImage( inImage, AffineTransform.getScaleInstance( scale, scale ), null );
        g2d.dispose();

        inImage = null;

        return outImage;
    }

    public static BufferedImage scaleIcon( BufferedImage inImage, int size )
    {
        final double scale = Math.min( (double) size / (double) inImage.getHeight( null ), (double) size
            / (double) inImage.getWidth( null ) );

        // Determine scaled size of new image.
        int scaledH, scaledW;
        if ( ( scaledW = (int) ( scale * inImage.getWidth( null ) ) ) <= 0 )
        {
            scaledW = 1;
        }
        if ( ( scaledH = (int) ( scale * inImage.getHeight( null ) ) ) <= 0 )
        {
            scaledH = 1;
        }

        final BufferedImage outImage = new BufferedImage( scaledW, scaledH, BufferedImage.TYPE_INT_RGB );
        final Graphics2D g2d;
        g2d = outImage.createGraphics();
        g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED );
        g2d.drawImage( inImage, AffineTransform.getScaleInstance( scale, scale ), null );
        g2d.dispose();

        return outImage;
    }

    public static Image scale( Image original, int maxSize )
    {
        return scale( original, maxSize, null );
    }

    public static Image scale( Image original, int maxSize, ImageObserver observer )
    {
        // default values, original image dimensions are used
        int width = -1;
        int height = -1;

        int originalWidth = original.getWidth( null );
        int originalHeight = original.getHeight( null );

        int max = Math.max( originalHeight, originalWidth );

        if ( max != maxSize )
        {
            if ( originalWidth == originalHeight )
            {
                width = maxSize;
                height = maxSize;
            }
            else
            {
                // TODO
            }
        }

        return original.getScaledInstance( width, height, Image.SCALE_SMOOTH );
    }

}
